﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Windows.Forms;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Gui;
using DBSourceToolsLib;
using DBSourceToolsLib.Composite;
using DBSourceToolsLib.Suggestions;
using ICSharpCode.MSSQLExtensions.SqlParser;


namespace TestICSharpCode.MSSQLExtensions
{
    [TestFixture]
    public class TestLineSegments
    {
        Form m_form = new Form();
        DBComposite m_DBComposite;

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            string strProjectFile = Constants.AdventureWorks_ProjectFile;
            DatabaseProject oDBProject = new DatabaseProject(strProjectFile);

            oDBProject.LoadFromDisk(false);
            Assert.AreEqual("AdventureWorks", oDBProject.ProjectName);
            Assert.Greater(oDBProject.SourceDatabases.Count, 0);
            Assert.AreEqual((string)"AdventureWorksLT", oDBProject.SourceDatabases[0].DatabaseName);
            Assert.AreEqual((string)"AdventureWorksLT", oDBProject.SourceDatabases[0].NickName);

            SourceDatabase oSource = oDBProject.SourceDatabases[0];

            System.Windows.Forms.Form oForm = new System.Windows.Forms.Form();

            SourceDatabaseLoader oSourceLoader =
                new SourceDatabaseLoader(oForm,
                    DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDisk);

            oSourceLoader.Execute(ref oSource, null);

            m_DBComposite = oSource.GetDBComposite();

        }


        [TestFixtureTearDown]
        public void TearDown()
        {

        }

        [STAThread]
        [Test]
        public void TestOpenDocument()
        {
            ICSharpCode.MSSQLExtensions.MSSQLTextEditor m_MsSqlTextEditor = new ICSharpCode.MSSQLExtensions.MSSQLTextEditor(m_form);
            m_MsSqlTextEditor.Text = "select *, field1, t.field1 from test t\n inner join dbo.TEST";

            TextWordParser oParser = new TextWordParser();
            oParser.SetTableNameList(new List<string> { "test" });
            oParser.SetFieldNameList(new List<string> { "field1" });

            foreach (ICSharpCode.TextEditor.Document.LineSegment oLineSegment in m_MsSqlTextEditor.Document.LineSegmentCollection)
            {
                foreach (ICSharpCode.TextEditor.Document.TextWord oWord in oLineSegment.Words)
                {
                    if (!oWord.IsWhiteSpace)
                    {
                        oParser.AddTextWord(oWord, oLineSegment.Offset);
                    }
                }
                //List<ICSharpCode.TextEditor.Document.TextWord> oWords = oLineSegment.Words;

            }

            Console.WriteLine(oParser.GetDebugString());
            //string strDebugString = oParser.GetDebugString();

            Assert.IsTrue(oParser.IsAfterWord(6), "IsAfterWord(6) should be true");
            Assert.AreEqual(oParser.PrevWord(6), "select");

            Assert.IsFalse(oParser.IsAfterWord(7), "IsAfterWord(6) should be false");

        }

        [STAThread]
        [Test]
        public void TestSuggestionBuilder_withComposite()
        {
            ICSharpCode.MSSQLExtensions.MSSQLTextEditor m_MsSqlTextEditor = new ICSharpCode.MSSQLExtensions.MSSQLTextEditor(m_form);
            m_MsSqlTextEditor.Text = "select from BuildVersion";

            SenseBuilder oSenseBuilder = new SenseBuilder(m_MsSqlTextEditor.Document);
            oSenseBuilder.SetComposite(m_DBComposite);
            List<Suggestion> oSenseList = oSenseBuilder.GetSuggestionsForCursorAt(6);
            List<string> lstExpected = new List<string> { "SystemInformationID" };

            Assert.Greater(oSenseList.Count(), 0, "Should return suggestions");
            Assert.True(SenseBuilder.SuggestionListToSimple(oSenseList).Contains("[Database Version]"));

        }

        [STAThread]
        [Test]
        public void TestSuggestionBuilder_SpeedTest()
        {
            ICSharpCode.MSSQLExtensions.MSSQLTextEditor m_MsSqlTextEditor = new ICSharpCode.MSSQLExtensions.MSSQLTextEditor(m_form);

            SenseBuilder oSenseBuilder = new SenseBuilder(m_MsSqlTextEditor.Document);
            oSenseBuilder.SetComposite(m_DBComposite);


            string strText = "";
            using (System.IO.StreamReader oReader = new System.IO.StreamReader(Constants.PARSE_LARGE_FILE))
            {
                strText = oReader.ReadToEnd();
            }

            m_MsSqlTextEditor.Text = strText;

            DateTime oDtStart = DateTime.Now;
            List<Suggestion> oSenseList = oSenseBuilder.GetSuggestionsForCursorAt(6);

            DateTime oDTEnd = DateTime.Now;
            TimeSpan oTimeSpan = oDTEnd - oDtStart;

            string strExecutionTime = string.Format("{0}ms", oTimeSpan.TotalMilliseconds);
            Console.WriteLine(strExecutionTime);

            Assert.Less(oTimeSpan.TotalMilliseconds, 100, strExecutionTime);

        }



    }
}
